# --*-- coding:utf-8 --*--
import warnings
warnings.filterwarnings("ignore")
import requests
import json
import datetime


class newTaobao():
	cookies = None
	head = {
				'Host': 'sycm.taobao.com',
				'Connection': 'keep-alive',
				'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
				'Accept': '*/*',
				'Accept-Encoding': 'gzip, deflate, br',
				'Accept-Language': 'zh-CN,zh;q=0.9'}

	def Category(self):
		## 获取账号类目数据
		url = 'https://sycm.taobao.com/mc/common/getShopCate.json?leaf=true&edition=std,pro,vip&_=1536547232616&token='
		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False)
		return  resp.json()

	def hotWord(self,cateId,time_interval):

		# 热搜词300
		dateType = 'day'
		dateRange = time_interval
		if time_interval == 7:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d")  #前一天
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 7)).strftime("%Y-%m-%d") # 7天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent7'
		if time_interval == 30:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") #
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 30)).strftime("%Y-%m-%d") # 30天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent30'
			
		url = 'https://sycm.taobao.com/mc/industry/searchWord.json'
		data = {
				'dateRange': dateRange,
				'dateType': dateType,
				'pageSize':'10',
				'page':'1',
				'order':'desc',
				'orderBy':'seIpvUvHits',
				'cateId':cateId,
				'device':'0',
				'indexCode':'hotSearchRank,seIpvUvHits,clickHits,clickRate,payRate',
				'_':'1536549668982',
				'token':''
		}
		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
		return resp.json()


	def tailWord(self,cateId,time_interval):

		# 长尾词300
		dateType = 'day'
		dateRange = time_interval
		if time_interval == 7:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d")  #前一天
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 7)).strftime("%Y-%m-%d") # 7天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent7'
		if time_interval == 30:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") #
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 30)).strftime("%Y-%m-%d") # 30天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent30'

		url = 'https://sycm.taobao.com/mc/industry/tailWord.json'
		data = {
				'dateRange': dateRange,
				'dateType': dateType,
				'pageSize':'10',
				'page':'1',
				'order':'desc',
				'orderBy':'seIpvUvHits',
				'cateId':cateId,
				'device':'0',
				'indexCode':'hotSearchRank,seIpvUvHits,clickHits,clickRate,payRate',
				'_':'1536549668982',
				'token':''
		}
		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
		return resp.json()

	def brandWord(self,cateId,time_interval):

		# 品牌词
		dateType = 'day'
		dateRange = time_interval
		if time_interval == 7:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d")  #前一天
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 7)).strftime("%Y-%m-%d") # 7天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent7'
		if time_interval == 30:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") #
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 30)).strftime("%Y-%m-%d") # 30天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent30'

		url = 'https://sycm.taobao.com/mc/industry/brandWord.json'
		data = {
				'dateRange': dateRange,
				'dateType': dateType,
				'pageSize':'10',
				'page':'1',
				'order':'desc',
				'orderBy':'avgWordSeIpvUvHits',
				'cateId':cateId,
				'device':'0',
				'indexCode':'hotSearchRank,relSeWordCnt,avgWordSeIpvUvHits,avgWordClickHits,avgWordClickRate,avgWordPayRate',
				'_':'1536549668982',
				'token':''
		}
		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
		return resp.json()

	def coreWord(self,cateId,time_interval):
		# 核心词
		dateType = 'day'
		dateRange = time_interval
		if time_interval == 7:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d")  #前一天
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 7)).strftime("%Y-%m-%d") # 7天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent7'
		if time_interval == 30:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") #
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 30)).strftime("%Y-%m-%d") # 30天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent30'

		url = 'https://sycm.taobao.com/mc/industry/coreWord.json'
		data = {
				'dateRange': dateRange,
				'dateType': dateType,
				'pageSize':'10',
				'page':'1',
				'order':'desc',
				'orderBy':'avgWordSeIpvUvHits',
				'cateId':cateId,
				'device':'0',
				'indexCode':'hotSearchRank,relSeWordCnt,avgWordSeIpvUvHits,avgWordClickHits,avgWordClickRate,avgWordPayRate',
				'_':'1536549668982',
				'token':''
		}
		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
		return resp.json()

	def attrWord(self,cateId,time_interval):
		# 修饰词
		dateType = 'day'
		dateRange = time_interval
		if time_interval == 7:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d")  #前一天
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 7)).strftime("%Y-%m-%d") # 7天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent7'
		if time_interval == 30:
			time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") #
			time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 30)).strftime("%Y-%m-%d") # 30天前
			dateRange = '%s|%s' %(time_str_N,time_str_p)
			dateType = 'recent30'

		url = 'https://sycm.taobao.com/mc/industry/attrWord.json'
		data = {
				'dateRange': dateRange,
				'dateType': dateType,
				'pageSize':'10',
				'page':'1',
				'order':'desc',
				'orderBy':'avgWordSeIpvUvHits',
				'cateId':cateId,
				'device':'0',
				'indexCode':'hotSearchRank,relSeWordCnt,avgWordSeIpvUvHits,avgWordClickHits,avgWordClickRate,avgWordPayRate',
				'_':'1536549668982',
				'token':''
		}
		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
		return resp.json()

	

if __name__ == '__main__':
	
	newtaobao = newTaobao()
	newtaobao.cookies = {"skt":"d17aad06d6eb017c","sg":"%E5%BA%9702","_cc_":"VT5L2FSpdA%3D%3D","csg":"1991d13e","tracknick":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","isg":"BGFhWDORnuvVaDKfibQhYF3zfCR7DtUAyo2PI8M2XWjHKoH8C17l0I_oiNCs-W04","cookie17":"UNN0nnQ5PzBj6g%3D%3D","JSESSIONID":"52E4FD43F7746623A58ADC5B0E7A0212","tg":"0","_l_g_":"Ug%3D%3D","lgc":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","dnk":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","cookie2":"18d50c164c3b1825f6bf7dd90a4a4a98","existShop":"MTU0MTEyNjc0NA%3D%3D","uc1":"cookie16=VFC%2FuZ9az08KUQ56dCrZDlbNdA%3D%3D&tmb=1&cookie21=URm48syIZxx%2F&cookie15=UIHiLt3xD8xYTw%3D%3D&existShop=true&pas=0&cookie14=UoTYN4A1oSPGOQ%3D%3D&tag=8&lng=zh_CN","cookie1":"UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D","v":"0","_nk_":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","mt":"np=","cna":"15ZiFBhnImkCAcFwhT7OdcJz","_tb_token_":"778b70e53e333","t":"0f4327a43a2f01009fa51445f4e5f2d9","uc3":"vt3=F8dByRjMmpCpGseoK80%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=V32FPkk%2Fw0dUvg%3D%3D","unb":"3397628220"}
	
	#类目数据
	# print(newtaobao.Category())

	#热搜词
	time_interval = 30
	cateId = '50013794'
	print (newtaobao.hotWord(cateId,time_interval))


	# #长尾词
	# time_interval = 7
	# cateId = '50000815'
	# print (newtaobao.tailWord(cateId,time_interval))

	#品牌词
	# time_interval = 7
	# cateId = '50000815'
	# print (newtaobao.brandWord(cateId,time_interval))

	#核心词
	# time_interval = 7
	# cateId = '50000815'
	# print (newtaobao.coreWord(cateId,time_interval))


	# 修饰词
	# time_interval = 7
	# cateId = '50000815'
	# print (newtaobao.attrWord(cateId,time_interval))